local guixin = fk.CreateSkill{
  name = "iberia__guixin"
}

Fk:loadTranslationTable{
  [guixin.name] = "归心",
  [":"..guixin.name] = "当你受到伤害后，你可以选择一项：1.摸一张牌／2.获得伤害来源牌。然后伤害来源角色可以令你执行另一项。",

  ["$iberia__guixin-draw"] = "摸一张牌",
  ["$iberia__guixin-obtain"] = "获得伤害来源牌",
  ["$iberia__guixin-op"] = "归心：可以令%src执行另一项（%arg）",
}

local choices = {
  "$iberia__guixin-draw",
  "$iberia__guixin-obtain",
  "Cancel"
}

local function doEffect(option, player, data)
  if player.dead then return end

  if option == choices[1] then
    player:drawCards(1, guixin.name)
  else
    if player.room:getCardArea(data.card) == Card.Processing then
      player.room:obtainCard(player, data.card, true, fk.ReasonJustMove, player, guixin.name)
    end
  end
end

guixin:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(guixin.name) and player == target
  end,
  on_cost = function (self, event, target, player, data)
    local options = table.simpleClone(choices)
    if not data.card or player.room:getCardArea(data.card) ~= Card.Processing then
      table.remove(options, 2)
    end

    local option = player.room:askToChoice(player, {
      all_choices = choices,
      choices = options,
      cancelable = true,
      skill_name = guixin.name,
    })
    if option == choices[3] then return end

    event:setCostData(self, {option = option, options = options})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local option = event:getCostData(self).option
    local options = table.simpleClone(event:getCostData(self).options)
    doEffect(option, player, data)
    if data.from and data.from:isAlive() then
      local another = table.find(options, function (o)
        return o ~= option and o ~= choices[3]
      end)

      if another and player.room:askToSkillInvoke(data.from, {
        skill_name = guixin.name,
        prompt = string.format("%s:%s::%s", "$iberia__guixin-op", player.id, another),
      }) then
        doEffect(another, player, data)
      end
    end
  end
})

return guixin